package com.ckd.flyingtrip.charge.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.ckd.flyingtrip.R;

import java.util.List;

public class BatteryTypeAdapter extends RecyclerView.Adapter<BatteryTypeAdapter.MyHolder> {

    private Context context;
    private List<String> list;
    private int positionSelect = 0;
    public OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public BatteryTypeAdapter(Context context, List<String> list) {
        this.context = context;
        this.list = list;
    }

    public void setList(List<String> list) {
        this.list = list;
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyHolder(LayoutInflater.from(context).inflate(R.layout.item_battery_recyclerview, null));
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {

        if ("0".equals(list.get(position))) {
            holder.textView.setText("磷酸铁锂电池");
        }else if ("1".equals(list.get(position))){
            holder.textView.setText("铅酸电池");
        }
        if (position == positionSelect) {
            holder.imageView.setBackground(ContextCompat.getDrawable(context, R.mipmap.choice));
        }else {
            holder.imageView.setBackground(ContextCompat.getDrawable(context, R.mipmap.not_selected));
        }
        holder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(v, position);
                positionSelect = position;
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        TextView textView;
        ImageView imageView;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.item_battery_recyclerview_textView);
            imageView = itemView.findViewById(R.id.iv_choose);
        }
    }
}
